package com.jayqqaa12.oauth2;

import com.jayqqaa12.Code;
import com.jayqqaa12.jbase.jfinal.ext.SpringUtils;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;

/**
 *
 */
public class OauthIntercepter implements Interceptor {

	private String key;

	public OauthIntercepter(String key) {
		this.key = key;
	}

	@Override
	public void intercept(Invocation ai) {

		if (ai.getActionKey().contains(key)) {

			Token tokenDao = SpringUtils.getBean(Token.class);
			String token = ai.getController().getPara("token");
		
			if (tokenDao.checkToken(token)) ai.invoke();
			else ai.getController().renderJson("{\"code\":" + Code.OAUTH2_TOEKN_INVALIVE.code + ",\"data\":{}}");
			
		}
		else ai.invoke();

	}

}
